﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace SmoothStreaming.PerformanceTesting.WinUI
{
    public class ChunkTestListViewItem : ListViewItem
    {
        public SmoothStreamChunk Chunk { get; private set; }
        public ChunkResponse ChunkTestResponse { get; private set; }
        private ChunkResponseAnalyzer _cra = new ChunkResponseAnalyzer();

        public ChunkTestListViewItem(SmoothStreamChunk chunk, ChunkResponse response)
        {
            Chunk = chunk;
            ChunkTestResponse = response;

            Refresh();
        }

        private void Refresh()
        {
            if (this.SubItems.Count <= 1)
            {
                AddSubItems(6);
            }

            SetSubItemText(0, Chunk.StreamType);
            SetSubItemText(1, (Chunk.Bitrate / 1000).ToString() + "kbps");
            SetSubItemText(2, Chunk.FragmentIndex.ToString());
            SetSubItemText(3, ChunkTestResponse.Elapsed.HasValue ? System.Math.Round(ChunkTestResponse.Elapsed.Value.TotalMilliseconds, 0).ToString() + "ms" : "unknown");
            SetSubItemText(4, Chunk.ChunkUrl);
            SetSubItemText(5, ChunkTestResponse.ReturnStatusCodeNumeric.ToString() + " / " + ChunkTestResponse.ReturnStatusCode.ToString());

            var responseRating = _cra.RateChunkResponse(Chunk, ChunkTestResponse);
            SetSubItemText(6, responseRating.ToString());

            SetColorForResponseRating(responseRating);
        }

        private void SetColorForResponseRating(ResponseRating responseRating)
        {
            switch (responseRating)
            {
                case ResponseRating.Excellent:
                    this.BackColor = Color.GreenYellow;
                    break;
                    
                case ResponseRating.Good:
                    this.BackColor = Color.LightGreen;
                    break;

                case ResponseRating.Warning:
                    this.BackColor = Color.Yellow;
                    break;

                case ResponseRating.Degraded:
                    this.BackColor = Color.LightCoral;
                    break;

                case ResponseRating.Poor:
                    this.BackColor = Color.Red;
                    this.ForeColor = Color.White;
                    break;

                case ResponseRating.Failed:
                    this.BackColor = Color.Black;
                    this.ForeColor = Color.White;
                    break;

                case ResponseRating.Unknown:
                    this.BackColor = Color.Gray;
                    break;

                default:
                    break;
            }
        }

        private void AddSubItems(int count)
        {
            for (int i = 0; i < count; i++)
            {
                this.SubItems.Add(string.Empty);
            }
        }

        private void SetSubItemText(int index, string text)
        {
            if (this.SubItems[index].Text == text)
            {
                return;
            }

            this.SubItems[index].Text = text;
        }
    }
}
